﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 **/

using System;
using System.ComponentModel.Composition;
using System.Windows.Input;
using Peter.Common;
using Peter.Common.Interfaces;
using Peter.Common.MainMenu;
using Peter.Common.Services;
using Peter.Common.Utilities;
using Peter.Ftp.View;

namespace Peter.Ftp.Logic
{
   /// <summary>
   /// Peter ftp support.
   /// </summary>
   public class PeterFtp : IDisposable
   {
      private IPeter m_Peter;
      private ModelFtp m_Ftp;
      private RelayCommand m_CmdFtpWindow;

      /// <summary>
      /// Initializes the peter ftp plugin.
      /// </summary>
      public PeterFtp ()
      {
         ViewRegistry.Register (typeof (ModelFtp), typeof (ViewFtp));
      }

      /// <summary>
      /// Gets the main ftp model.
      /// </summary>
      [Export (typeof (PeterDockItem))]
      [ExportMetadata ("ContentId", ModelFtp.GUID)]
      public ModelFtp Ftp
      {
         get { return this.m_Ftp ?? (this.m_Ftp = new ModelFtp ()); }
      }

      /// <summary>
      /// Gets the command that shows/hides the ftp window.
      /// </summary>
      public ICommand FtpWindowCommand
      {
         get { return this.m_CmdFtpWindow ?? (this.m_CmdFtpWindow = new RelayCommand (this.OnFtpWindow)); }
      }

      /// <summary>
      /// Occurs when the ftp window button is clicked.
      /// </summary>
      /// <param name="obj">Command parameter.</param>
      private void OnFtpWindow (object obj)
      {
         if (this.m_Peter == null)
         {
            this.m_Peter = ServiceManager.Get<IPeter> ();
            this.m_Peter.ShuttingDown += this.OnShutDown;
         }
         this.m_Peter.AddToolWindow (this.Ftp);
      }

      /// <summary>
      /// Gets the show ftp menu item.
      /// </summary>
      [Export (typeof (IMainMenuItem))]
      public IMainMenuItem FtpMenuItem
      {
         get
         {
            return new PeterMainMenuItem
            {
               Priority = 10,
               HeaderName = "view",
               Command = this.FtpWindowCommand,
               ToolTip = "Show FTP Window",
               Icon = MainMenuIcon.Ftp
            };
         }
      }

      /// <summary>
      /// Occurs when peter is shutting down.
      /// </summary>
      /// <param name="sender">Peter interface.</param>
      /// <param name="e">EventArgs</param>
      private void OnShutDown (object sender, EventArgs e)
      {
         this.Dispose ();
      }

      /// <summary>
      /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
      /// </summary>
      /// <filterpriority>2</filterpriority>
      public void Dispose ()
      {
         Dispose (true);
         GC.SuppressFinalize (this);
      }

      /// <summary>
      /// Disposes the object.
      /// </summary>
      /// <param name="disposing">True to release managed resources.</param>
      private void Dispose (bool disposing)
      {
         if (disposing)
         {
            if (this.m_Ftp != null)
               this.Ftp.Dispose ();
         }
      }
   }
}
